'use strict';

const Controller = require('egg').Controller;

class ResumeProcessingController extends Controller {
  // 获取简历处理列表
  async index() {
    const { ctx } = this;
    const { user_id = 1, page = 1, pageSize = 10 } = ctx.query;
    
    try {
      const result = await ctx.service.resumeProcessing.findAll({
        user_id,
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      });
      
      ctx.body = {
        code: 200,
        message: '获取简历处理列表成功',
        data: result.data,
        pagination: result.pagination,
      };
    } catch (error) {
      ctx.logger.error('获取简历处理列表失败:', error);
      ctx.body = {
        code: 500,
        message: '获取简历处理列表失败',
        data: null,
      };
    }
  }

  // 获取简历处理详情
  async show() {
    const { ctx } = this;
    const { id } = ctx.params;
    
    try {
      const record = await ctx.service.resumeProcessing.findById(id);
      if (record) {
        ctx.body = {
          code: 200,
          message: '获取简历处理详情成功',
          data: record,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '记录不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('获取简历处理详情失败:', error);
      ctx.body = {
        code: 500,
        message: '获取简历处理详情失败',
        data: null,
      };
    }
  }

  // 创建简历处理记录
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    
    try {
      const record = await ctx.service.resumeProcessing.create(data);
      ctx.body = {
        code: 200,
        message: '创建记录成功',
        data: record,
      };
    } catch (error) {
      ctx.logger.error('创建记录失败:', error);
      ctx.body = {
        code: 500,
        message: '创建记录失败',
        data: null,
      };
    }
  }

  // 更新简历处理记录
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const data = ctx.request.body;
    
    try {
      const record = await ctx.service.resumeProcessing.update(id, data);
      if (record) {
        ctx.body = {
          code: 200,
          message: '更新记录成功',
          data: record,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '记录不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('更新记录失败:', error);
      ctx.body = {
        code: 500,
        message: '更新记录失败',
        data: null,
      };
    }
  }

  // 删除简历处理记录
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;
    
    try {
      const result = await ctx.service.resumeProcessing.delete(id);
      if (result) {
        ctx.body = {
          code: 200,
          message: '删除记录成功',
          data: null,
        };
      } else {
        ctx.body = {
          code: 404,
          message: '记录不存在',
          data: null,
        };
      }
    } catch (error) {
      ctx.logger.error('删除记录失败:', error);
      ctx.body = {
        code: 500,
        message: '删除记录失败',
        data: null,
      };
    }
  }
}

module.exports = ResumeProcessingController;
